﻿Imports Co9320.NamaAlert.ViewModel
Imports System.Collections.ObjectModel

Namespace Behavior

    Public Class ProgramContextMenuBehavior

#Region "ItemsSource"
        Private Shared MenuItems As ObservableCollection(Of LiveProgramViewModel)

        Public Shared Function GetItemsSource(ByVal element As DependencyObject) As ObservableCollection(Of LiveProgramViewModel)
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            Return DirectCast(element.GetValue(ItemsSourceProperty), ObservableCollection(Of LiveProgramViewModel))
        End Function

        Public Shared Sub SetItemsSource(ByVal element As DependencyObject, ByVal value As ObservableCollection(Of LiveProgramViewModel))
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            element.SetValue(ItemsSourceProperty, value)
        End Sub

        Public Shared ReadOnly ItemsSourceProperty As  _
                               DependencyProperty = DependencyProperty.RegisterAttached("ItemsSource", _
                               GetType(ObservableCollection(Of LiveProgramViewModel)), GetType(ProgramContextMenuBehavior), _
                                New UIPropertyMetadata(Nothing, AddressOf OnItemsSourcePropertyChanged))


        Public Shared Sub OnItemsSourcePropertyChanged(ByVal element As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
            If Not TypeOf e.NewValue Is ObservableCollection(Of LiveProgramViewModel) Then
                Exit Sub
            End If
            MenuItems = DirectCast(e.NewValue, ObservableCollection(Of LiveProgramViewModel))
            ConstructMenu(element)
        End Sub
#End Region

#Region "HeaderItems"
        Private Shared HeaderItems As Object()

        Public Shared Function GetHeaderItems(ByVal element As DependencyObject) As Object()
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            Return DirectCast(element.GetValue(HeaderItemsProperty), Object())
        End Function

        Public Shared Sub SetHeaderItems(ByVal element As DependencyObject, ByVal value As Object())
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            element.SetValue(HeaderItemsProperty, value)
        End Sub

        Public Shared ReadOnly HeaderItemsProperty As  _
                               DependencyProperty = DependencyProperty.RegisterAttached("HeaderItems", _
                               GetType(Object()), GetType(ProgramContextMenuBehavior), _
                               New UIPropertyMetadata(Nothing, AddressOf OnHeaderItemsSourcePropertyChanged))

        Public Shared Sub OnHeaderItemsSourcePropertyChanged(ByVal element As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
            If Not TypeOf e.NewValue Is Object() Then
                Exit Sub
            End If
            HeaderItems = DirectCast(e.NewValue, Object())
            ConstructMenu(element)
        End Sub
#End Region

#Region "FooterItems"
        Private Shared FooterItems As Object()

        Public Shared Function GetFooterItems(ByVal element As DependencyObject) As Object()
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            Return DirectCast(element.GetValue(HeaderItemsProperty), Object())
        End Function

        Public Shared Sub SetFooterItems(ByVal element As DependencyObject, ByVal value As Object())
            If element Is Nothing Then
                Throw New ArgumentNullException("element")
            End If

            element.SetValue(HeaderItemsProperty, value)
        End Sub

        Public Shared ReadOnly FooterItemsProperty As  _
                               DependencyProperty = DependencyProperty.RegisterAttached("FooterItems", _
                               GetType(Object()), GetType(ProgramContextMenuBehavior), _
                               New UIPropertyMetadata(Nothing, AddressOf OnHeaderItemsSourcePropertyChanged))

        Public Shared Sub OnFooterItemsSourcePropertyChanged(ByVal element As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
            If Not TypeOf e.NewValue Is Object() Then
                Exit Sub
            End If
            FooterItems = DirectCast(e.NewValue, Object())
            ConstructMenu(element)
        End Sub
#End Region

        Private Shared Sub ConstructMenu(ByVal element As DependencyObject)
            Dim menu = TryCast(element, ContextMenu)
            If menu Is Nothing Then
                Exit Sub
            End If
            menu.Items.Clear()

            If Not HeaderItems Is Nothing Then
                For Each o In HeaderItems
                    menu.Items.Add(o)
                Next
            End If

            If Not MenuItems Is Nothing Then
                For Each m In MenuItems
                    ' TODO User Control を追加
                    menu.Items.Add(New MenuItem With {.Header = m.Title, .Icon = m.Icon})
                Next
            End If

            If Not FooterItems Is Nothing Then
                For Each o In FooterItems
                    menu.Items.Add(o)
                Next
            End If
        End Sub
    End Class

End Namespace
